<?php
// this page includes the server side of application, exluding the admin part, sent to the client and displayed
session_start();
if (!isset($_POST['do']))
	{
		exit;
	}
?>
<script src="inc/js/core.js" type="text/javascript"></script>
<script src="inc/js/jquery.timeago.js" type="text/javascript"></script>

<?php	
require ('./inc/database.php');
	require ('./inc/functions.php');
	require ('./config.php');
	require_once('./class/class.phpmailer.php');
	require_once('./class/class.user.php');
	require_once('./class/class.project.php');
	require_once('./class/class.task.php');
	require_once('./class/class.project_access_details.php');
	require_once('./class/class.permissions.php');
	require_once('./class/class.comments.php');
	require_once('./class/class.dashboard.php');
	db_connect();	
	
	
if($_POST['do']=='get_new_task')//returns all the tasks that needs to be move to recived by admin
{
   $arr_task=task::get_all_task();//all system tasks 
	 $temp=0;
	
	 ?>
    
     <?php 
	 dashboard::get_html_bash();
	   foreach($arr_task as $task)
		{
			$project_name=$task['project_name'];
			$customer_name=$task['customer_name'];
			$task=task::createTaskFromArray($task);
		    if($task->task_status_id==1)
			   {
				  if($temp==$task->project_id)
				   {	
					  $task->get_html_task();	
				   }
				  else 
				  	{?>
                        </div>
					 	<div class="project_tasks_container" id="<?php echo $task->project_id ?>">
                        <?php
                        	
					 		?> <p class=" project_title admin_project_title">
                             <?php echo $project_name.'-'.$customer_name; ?>
                             </p>
						<?php
							$task->get_html_task();	
					  ?>
		    	<?php }// else
				$temp=$task->project_id;
				}// if task status==1 
			 
		}// foreach
	?> </div><?php	
}//get new task

if($_POST['do']=='get_tasks_open_admin')//all open tasks for admin
{
   $arr_task=task::get_all_task();//all tasks
	 $temp=0;
	 ?>
      <p>'פניות פתוחות'</p>     
      
     <?php 
	   foreach($arr_task as $task)
		{
			$project_name=$task['project_name'];
			$customer_name=$task['customer_name'];
			$task=task::createTaskFromArray($task);
			if($task->task_status_id==2 || $task->task_status_id==3 )
				{
				  if($temp==$task->project_id)
				   {	
					 // $project=new project();
					  //$project->getby_id($task->project_id);
					  $task->get_html_task();	
				   }
				  else 
				  	{?>
                        </div>
					 	<div class="project_tasks_container" id="<?php echo $task->project_id ?>">
                        <?php
                        	//$project=new project();
					  		//$project->getby_id($task->project_id);
					 		?> <p class=" project_title admin_project_title">
                             <?php echo $project_name.'-'.$customer_name; ?>
                             </p>
							<?php
							$task->get_html_task();	
					  ?>
		    	<?php }
				$temp=$task->project_id;
				}
			
		}
		?> </div><?php
}//get_tasks_open_admin
if($_POST['do']=='get_from_dashboard')
{
 	$id=$_POST['id'];
	        $today=date('Y-m-d');
			$temp=strtotime($today);
 			$cov=strtotime("  Last Sunday", $temp);
 			$new=date('Y-m-d',$cov);
			$temp=strtotime($new);
			$cov=strtotime("+6 days", $temp);
			$end=date('Y-m-d',$cov);
	switch($id)
	 {
	  case 1:
	    {
			$st=sprintf("SELECT * FROM `project` WHERE `project_status`=2");
		 break;	
		}
		 case 2:
	    {
			$st=sprintf("SELECT * FROM `project` WHERE `project_status`=3");
		 break;	
		}
		 case 3:
	    {
			$st=sprintf("SELECT * FROM `task` WHERE  `task_status_id`=1 and task_date BETWEEN '$new' and '$end' order by project_id desc  ");
		 break;	
		}
		 case 4:
	    {
		   $st=sprintf("SELECT * FROM `task` WHERE  `task_status_id`=2 or `task_status_id`=3 and task_date BETWEEN '$new' and '$end' order by project_id desc ");
		 break;	
		}
		 case 5:
	    {$st=sprintf("SELECT * FROM `task` WHERE  `task_status_id`=4 and task_date BETWEEN '$new' and '$end' order by project_id desc ");
			
		 break;	
		}
		case 6:
		$st=sprintf("SELECT * FROM `project` WHERE `project_status`=1");
		 break;
		 
	 }
	 $query=mysql_query($st) or die (mysql_error(). ' '.$st);
	 dashboard::get_html_bash();
	 if($id==1 || $id==2 || $id=3)
	   {
		   $project = new project();
		  while ($row=mysql_fetch_assoc($query)) 
		   {
		     $project->getby_id($row['project_id']);
			 ?><span id="name_project" class="color_w"><?php echo  $project->project_name ?></span> <?php
			 $project->get_html_project();
			 echo "<div class='br'></div>";
		   }
	   }
	 else 
	  	{
			 $query=mysql_query($st) or die (mysql_error(). ' '.$st);
	 		 $temp = 0;
			  while ($row=mysql_fetch_assoc($query)) 
			   { 
			         $task = new task();
					 $task->getby_id($row['task_id']);
				  if($temp==$row['project_id']) 
				   {
					 $task->get_html_task();
					 echo "<div class='br'></div>";   
				   }
				  else
				    {?>
					 	     <div class="project_tasks_container" id="<?php echo $row['project_id']?>">
                      <?php
                        	 $project=new project();
					  		 $project->getby_id($row['project_id']);
					  ?>     <p class=" project_title admin_project_title">
                       <?php echo $project->project_name.'-'.$project->get_customer_name($project->project_customer); ?>
                             </p>
					<?php
							 $task->get_html_task();
					}
				  $temp=$row['project_id'];	
			   }
		}
	
}//get_from_dashboard
if($_POST['do']=='find_for_task')
{
	$session=$_SESSION['user'];
	$user= new user();
	$user->getby_name($session);
	$task = new task();
    $task->getby_id($_POST['id_task']);
 if($task->get_task_id()=="" || (!(permissions::is_permissions($user->getuser_id(),$task->project_id)) && ($user->role_type==4 || $user->role_type==5 )) )//אם אין מספר פנייה 
    {
	 echo "מספר הפנייה לא נמצא במערכת";	
	}
  else
  	{
		$project = new project();
		$project->getby_id($task->project_id);
         if($project->project_status!=1 || isset($_SESSION['Admin']))
		  {
	    		?><p class=" project_title admin_project_title">
			   <?php echo $project->project_name.'-'.$project->get_customer_name($project->project_customer); ?>
			   </p>
	   			 <?php
			   $task->get_html_task();
	      }
		  else 
		    {
				 echo "מספר הפנייה לא נמצא במערכת";	
			}
	 }
}//find_for_task

	
	
	?>